package com.glory.brepda.activity.mfg;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MaterielAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.EqpEntity;
import com.glory.brepda.entity.LotAndPackagingfileEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.ScrapEntity;
import com.glory.brepda.entity.WoPartWobomline;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class StripPrint2Activity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvWoID)
    TextView tvWoID;
    @BindView(R.id.etRlfInfo)
    FreeUI_ClearEditText etRlfInfo;
    @BindView(R.id.tvPrintNum)
    TextView tvPrintNum;
    @BindView(R.id.tvRLF)
    TextView tvRLF;
    @BindView(R.id.rvMaterielList)
    RecyclerView rvMaterielList;
    private MaterielAdapter materielAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_strip_print2;
    }

    @Override
    protected void initView() {

        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvBaseSubmit.setText("提交");
        Bundle bundle = getIntent().getExtras();
        tvWoID.setText(bundle.getString("WOID"));
        tvRLF.setText(bundle.getString("tvRLF"));
        tvPrintNum.setText(bundle.getString("PRINTCOUNT"));

        tvTitle.setText(title);
        setMainLayout(llMain);

        initRecyclerView(rvMaterielList, this);
        materielAdapter = new MaterielAdapter(null);
        materielAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        materielAdapter.isFirstOnly(true);
        rvMaterielList.setAdapter(materielAdapter);
        etRlfInfo.requestFocus();
        initEdit(etRlfInfo,s ->{
            EqpEntity.EquipmentlistBean item=new EqpEntity.EquipmentlistBean();
            item.setStatusmodelrrn(getText(etRlfInfo));
            materielAdapter.addData(materielAdapter.getData().size(), item);
            etRlfInfo.setText("");
            etRlfInfo.postDelayed(() -> {
                etRlfInfo.requestFocus();
            }, 100);


        });

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (materielAdapter.getData().size()==0){
                    showToast("请扫描框架二维码",true);
                    return;
                }

                FreeUI_EntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>>
                        (new TypeToken<BaseResponseBean<LotAndPackagingfileEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showToast("打印成功", true);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "失败");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("WOID", tvWoID.getText().toString());
                map.addProperty("ACTIONTYPE", "PRINT");
                map.add("RLFINFO", new JsonParser().parse(new Gson().toJson(materielAdapter.getData())).getAsJsonArray());
                map.addProperty("PRINTCOUNT", tvPrintNum.getText().toString());
                NetUtils.requestPostNet("IF.WIP_WO_STRIPID", "Common", getApplication(), map, callBack);

                break;

        }
    }
}
